package ai.people.netmon.msgsearch.controller;

import ai.people.core.common.properties.elastic.ElasticIndex;
import ai.people.netmon.framework.domain.searchmsg.request.DataQuery;
import ai.people.netmon.framework.domain.searchmsg.request.MessageRequest;
import ai.people.netmon.framework.domain.searchmsg.vo.SimpleLabelValueVO;
import ai.people.netmon.framework.model.response.ResponseResult;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.msgsearch.service.EsSearchService;
import ai.people.netmon.msgsearch.service.MessageSearchService;
import ai.people.netmon.msgsearch.service.SearchAfterServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @author: Xing.Wei
 * @time: 2020-12-26 14:17
 */
@RefreshScope
@RestController
@RequestMapping("v2")
public class MsgSearchController {

    @Autowired
    private MessageSearchService messageSearchService;

    @Autowired
    private EsSearchService esSearchServiceImpl;

    @Autowired
    private SearchAfterServiceImpl searchAfterServiceImpl;

    @Autowired
    private ElasticIndex elasticIndex;


    /**
     * 提供隐网系统搜索模块检索接口
     * @param response
     * @param infoDataQuery
     * @return
     */
    @GetMapping("search")
    public Result search(HttpServletResponse response, DataQuery infoDataQuery) {
        String stream = infoDataQuery.getStream();
        if (StringUtils.isNotBlank(stream) && "1".equals(stream.trim())) {
            //return messageSearchService.searchByStream(response, infoDataQuery);
            return searchAfterServiceImpl.searchByStream(response, infoDataQuery);
        } else {
            return searchAfterServiceImpl.search(infoDataQuery, false);
        }
    }

    /**
     * 获取隐网系统搜索模块已存在信源名称列表
     * @return
     */
    @GetMapping("sources")
    public Result<List<SimpleLabelValueVO<String, String>>> sources() {
        List<SimpleLabelValueVO<String, String>> vos = Lists.newArrayList();
        List<String> sourceNames = elasticIndex.getAllInformationSourceNames();
        sourceNames.forEach(o->{
            if(o.equalsIgnoreCase("whatsapp")){
                vos.add(new SimpleLabelValueVO<>("WhatsApp",o));
            }else {
                char[] chars = o.toCharArray();
                chars[0] -= 32;
                vos.add(new SimpleLabelValueVO<>(new String(chars),o));
            }
        });
        return new Result<>(vos);
    }


    /**
     * 搜索
     *
     * @param messageRequest 消息请求
     * @return {@link ResponseResult}
     */
    @PostMapping("/search")
    public Result search(MessageRequest messageRequest){
        return esSearchServiceImpl.search(messageRequest,false);
    }


    @PostMapping("/search2")
    public Result search2(MessageRequest messageRequest){
        return esSearchServiceImpl.search(messageRequest, true);
    }

}
